﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using Salon.Properties;
using Salon.Reports;
using System.Data.SqlClient;

namespace Salon
{
    public partial class ReportCars : Form
    {
        public ReportCars()
        {
            InitializeComponent();
        }

        private void ReportCars_Load(object sender, EventArgs e)
        {
            this.cmbColor.Initialize();
            this.cmbMark.Initialize();
            //this.cmbModel.Initialize();
            cmbModel.Enabled = false;
            this.cmbSalon.Initialize();
            for (int i = 1950; i <= DateTime.Now.Year; i++)
                this.cmbYear.Items.Add(i);

            this.cmbColor.ColumnName = "colors.id";
            this.cmbColor.Precondition = "car.color=colors.id";

            this.cmbMark.ColumnName = "mark.id";
            this.cmbMark.Precondition = "car.mark=mark.id";

            this.cmbModel.ColumnName = "models.id";
            this.cmbModel.Precondition = "car.type=models.id";

            this.cmbYear.ColumnName = "car.year";

            this.cmbSalon.ColumnName = "salon.id";
            this.cmbSalon.Precondition = "car.salon=salon.id";

            this.cmbCarsOrder.Items.Add(new SortOrderItem("car.id","ID"));
            this.cmbCarsOrder.Items.Add(new SortOrderItem("mark.value", "Marka"));
            this.cmbCarsOrder.Items.Add(new SortOrderItem("models.value", "Model"));
            this.cmbCarsOrder.Items.Add(new SortOrderItem("car.year", "Rok"));
            this.cmbCarsOrder.Items.Add(new SortOrderItem("colors.value", "Kolor"));
            this.cmbCarsOrder.Items.Add(new SortOrderItem("salon.name", "Salon"));
            this.cmbCarsOrder.ValueMember = "Val";
            this.cmbCarsOrder.DisplayMember = "Display";
            this.cmbCarsOrder.SelectedIndex = 0;

            cmbMark.SelectedIndexChanged += cmbMark_SelectedValueChanged;
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            double from;
            double to;
            
            
            
            String query = "SELECT car.id," +
                "mark.value as mark," +
                "models.value as model," +
                "colors.value as color," +
                "car.year," +
                "car.cost," +
                "car.price," +
                "car.sold," +
                "salon.name as salon FROM car,models,colors,mark,salon WHERE 1=1";
            String condition = Global.getConditionString(Global.getFilterList(this));
            query += condition;

            if(!txtTo.Text.Equals("") || !txtFrom.Text.Equals(""))
            {
                try
                {
                    to = double.Parse(txtTo.Text);
                    from = double.Parse(txtFrom.Text);
                }
                catch (System.Exception e2)
                { 
                    MessageBox.Show("W pola Od i Do proszę wpisać liczby lu zostawić puste");
                    return;
                }
                query += " AND car.price between " + from.ToString().Replace(",", ".") + " AND " + to.ToString().Replace(",", ".");
            }

            if (rbFree.Checked)
                query += " AND car.sold=0";
            else if (rbSold.Checked)
                query += " AND car.sold=1";

            query += " ORDER BY " + cmbCarsOrder.SelectedItem.ToString();
            
            DataSet ds = new DataSet("DSCars");
            SqlDataAdapter da = new SqlDataAdapter(query, SqlService.getInstance().getConnection());
            da.TableMappings.Add("Table", "Cars");
            da.Fill(ds);
            
            RPTCars report = new RPTCars();
            
            report.SetDataSource(ds);
            report.SummaryInfo.ReportTitle = "Samochody";
            
            ReportViewer rv = new ReportViewer();
            rv.SetSource(report);
            rv.Show();
        }

        private void cmbMark_SelectedValueChanged(object sender, EventArgs e)
        {
            // Aktualizacja modelu elementami z danej marki
            if (cmbMark.SelectedValue != null)
            {
                cmbModel.InitializeMark((int)cmbMark.SelectedValue);
                cmbModel.Enabled = true;
                cmbModel.SelectedItem = null;
            }
            else
            {
                cmbModel.Enabled = false;
                cmbModel.SelectedItem = null;
            }
        }

       
    }
}
